package com.xh.petdogexpert.user.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qiniu.QiNiuUpload;
import com.xh.petdogexpert.base.BaseController;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.base.ResponseData;
import com.xh.petdogexpert.user.dto.User;
import com.xh.petdogexpert.user.service.IUserService;

@Controller
public class UserController extends BaseController {

	@Autowired
	IUserService userService;

	final static Logger logger = LoggerFactory.getLogger(UserController.class);

	@RequestMapping(value = "user/queryUsers")
	@ResponseBody
	public ResponseData queryUsers(User user) {

		ResponseData rData = new ResponseData();
		List<User> users = userService.queryUsers(user);
		rData.setData(users);
		return rData;
	}

	@RequestMapping(value = "user/getUser")
	@ResponseBody
	public ResponseData getUser(User user) throws BaseException {

		ResponseData rData = new ResponseData();
		User rUser = null;

		rUser = userService.getUser(user);
		rData.setData(rUser);

		return rData;
	}

	@RequestMapping(value = "user/getMyInfo")
	@ResponseBody
	public ResponseData getMyInfo() throws BaseException {

		ResponseData rData = new ResponseData();
		User rUser = null;

		rUser = userService.getMyInfo();
		rUser.setDogs(userService.getMyDogs());
		rData.setData(rUser);

		return rData;
	}

	@RequestMapping(value = "user/addUpdateUser")
	@ResponseBody
	public ResponseData addUpdateUser(User user) throws BaseException {

		ResponseData rData = new ResponseData();

		int id = userService.addUpdateUser(user);
		if (user.getId() != null) {
			rData.setMsg("修改用户成功");
		} else {
			rData.setMsg("新增用户成功");
		}
		rData.data = id;

		return rData;
	}

	@RequestMapping(value = "user/register")
	@ResponseBody
	public ResponseData register(User user) throws BaseException {
		ResponseData rData = new ResponseData();

		int id = userService.register(user);

		rData.setMsg("注册成功");
		rData.data = id;

		return rData;
	}

	@RequestMapping(value = "user/forgetPassword")
	@ResponseBody
	public ResponseData forgetPassword(String email) throws Exception {

		ResponseData rData = new ResponseData();

		userService.forgetPassword(email);
		rData.setData(true);

		return rData;
	}

	@RequestMapping(value = "user/resetPassword2")
	@ResponseBody
	public ResponseData resetPassword2(String nickname, String email, String password) throws Exception {

		String msg = userService.resetPassword(nickname, email, password);

		return new ResponseData(msg);
	}

	@RequestMapping(value = "user/resetPassword")
	@ResponseBody
	public ResponseData resetPassword(@RequestParam(required = true) String key,
			@RequestParam(required = true) String password) throws Exception {

		String msg = userService.resetPassword(password, key);

		return new ResponseData(msg);
	}

	@RequestMapping(value = "user/resetPasswordShow")
	public ModelAndView resetPasswordShow(String key) throws Exception {

		String email = userService.resetPasswordShow(key);
		ModelAndView model = new ModelAndView();
		model.addObject("email", email);
		model.setViewName("user/reset_password");
		return model;
	}

	@RequestMapping(value = "user/deleteUser")
	@ResponseBody
	public ResponseData deleteUser(User user) throws BaseException {

		ResponseData rData = new ResponseData();

		int count = userService.deleteUser(user);
		rData.setData(count);

		return rData;
	}

	@Autowired
	QiNiuUpload qiNiuUpload;

	@Autowired
	HttpSession session;

	/*
	 * 设置用户的详细信息，普通会员或者兽医会有详细的信息
	 */
	@RequestMapping(value = "user/dogupload")
	public ModelAndView dogUpload(Integer userId) throws BaseException {
		// if(userId == null)
		// throw new BaseException("没有参数，出错", BaseException.HTML_ERROR);
		// System.out.println("userID = " + userId);

		ModelAndView view = new ModelAndView("web-cilent/dogupload");
		User user = (User) session.getAttribute("user");
		String fileName = user.getId() + "" + new Date().getTime();
		String uploadToken = qiNiuUpload.getUpToken(fileName);
		view.addObject("uploadToken", uploadToken);
		view.addObject("fileName", fileName);
		return view;
	}

	/*
	 * 设置用户的详细信息，普通会员或者兽医会有详细的信息
	 */
	@RequestMapping(value = "user/member_edit")
	public ModelAndView memberEdit(Integer userId) throws BaseException {
		// if(userId == null)
		// throw new BaseException("没有参数，出错", BaseException.HTML_ERROR);
		// System.out.println("userID = " + userId);

		ModelAndView view = new ModelAndView("user/member_edit");
		view.addObject("userId", userId);

		return view;
	}

	@RequestMapping(value = "user/getMyDogs")
	@ResponseBody
	public ResponseData getMyDogs() {
		return new ResponseData(userService.getMyDogs());
	}
	

	@RequestMapping(value = "user/saveDogPic")
	@ResponseBody
	public ResponseData saveDogPic(String picName) {
		userService.saveDogPic(picName);
		return new ResponseData(true);
		
	}
	
	@RequestMapping(value = "user/getDogPic")
	@ResponseBody
	public ResponseData getDogPic() {
		
		return new ResponseData(userService.getDogPic());
		
	}
}
